package com.shm.client;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class DownThread extends Thread {
	String mAddr;
	String mResult;

	DownThread(String addr) {
		mAddr = addr;
	}

	public void run() {
		HttpGet get = new HttpGet(mAddr);
		DefaultHttpClient client = new DefaultHttpClient();
		try {
			client.execute(get, mResHandler);
		} catch (Exception e) {
			;
		}
	}

	ResponseHandler<String> mResHandler = new ResponseHandler<String>() {
		public String handleResponse(HttpResponse response) {
			StringBuilder html = new StringBuilder();
			try {
				BufferedReader br = new BufferedReader(new InputStreamReader(
						response.getEntity().getContent()));
				for (;;) {
					String line = br.readLine();
					if (line == null)
						break;
					html.append(line + '\n');
				}
				br.close();

				Message message = mAfterDown.obtainMessage();
				Bundle bundle = new Bundle();
				bundle.putString("result", html.toString());
				message.setData(bundle);
				mAfterDown.sendMessage(message);
			} catch (Exception e) {
				;
			}
			return html.toString();
		}
	};

	Handler mAfterDown = new Handler() {
		public void handleMessage(Message msg) {
//			mProgress.dismiss();
			mResult = msg.getData().getString("result");
			Log.i("shm", mResult);
		}
	};
}
